package com.asm.controller.api;

import com.asm.controller.BaseController;
import com.asm.dto.PageResult;
import com.asm.dto.Result;
import com.asm.service.WorkOrderService;
import com.asm.vo.sys.WorkOrderVo;
import io.swagger.annotations.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author morningbear
 * @description 工单相关的路由
 * @date 2019-07-17 16:29
 **/
@RestController
@Api(value = "工单Controller")
public class WorkOrderController extends BaseController {
    @Resource
    private WorkOrderService workorderService;

    /**
     * 分页查询所有合同
     *
     * @param pageNumber 页数
     * @param pageSize   页大小
     * @return 相应信息
     */
    @ApiOperation("接口描述：分页查询所有合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", value = "页数"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")
    })
    @GetMapping("/workorders")
    public ResponseEntity<PageResult<WorkOrderVo>> getAllWorkOrders(Integer pageNumber, Integer pageSize) {
        return new ResponseEntity(new Result().ok("success", workorderService.getWorkOrderByStatusSort(pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 根据Id获取合同
     *
     * @param id 编号
     * @return 相应信息
     */
    @ApiOperation("接口描述：根据Id获取合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户编号")
    })
    @GetMapping("/workorder/id/{id}")
    public ResponseEntity<Result<WorkOrderVo>> getWorkOrderById(@PathVariable("id") Long id) {
        return new ResponseEntity(new Result().ok("success", workorderService.findWorkOrderById(id)), HttpStatus.OK);
    }

    /**
     * 根据工人Id获得该工人的所有合同
     *
     * @param workerid 工单编号
     * @return 相应信息
     */
    @ApiOperation("接口描述：根据工人Id获取该工人的所有合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workerid", value = "工人id"),
            @ApiImplicitParam(name = "pageNumber", value = "页数"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")
    })
    @GetMapping("/workorder/workerid/{workerid}")
    public ResponseEntity<Result<WorkOrderVo>> getWorkOrderByWorkerId(@PathVariable("workerid") Long workerid, Integer pageNumber, Integer pageSize) {
        return new ResponseEntity(new Result().ok("success", workorderService.findWorkOrderByWorkerId(workerid, pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 根据业务员Id获得该业务员审批过的所有合同
     *
     * @param sellId     业务员Id
     * @param pageNumber 页数
     * @param pageSize   页大小
     * @return 相应信息
     */
    @ApiOperation("接口描述：根据业务员Id获得业务员审批过的所有的合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sellId", value = "业务员id"),
            @ApiImplicitParam(name = "pageNumber", value = "页数"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")
    })
    @GetMapping("/workorder/sellId/{sellId}")
    public ResponseEntity<PageResult<Map<String, Object>>> getWorkOrderBySellId(@PathVariable("sellId") Long sellId, @RequestParam(value = "pageNumber", defaultValue = "0") Integer pageNumber, @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize) {
        return new ResponseEntity(new Result().ok("success", workorderService.findWorkOrderBySellId(sellId)), HttpStatus.OK);
    }

    /**
     * 更新工单
     *
     * @param workOrderVo 工单
     * @return 相应信息
     */
    @ApiOperation("接口描述：更新工单")
    @PutMapping("/workorder/update")
    public ResponseEntity<Result> updateWorkOrder(@RequestBody @ApiParam(name = "workOrderVo", value = "工单视图对象") WorkOrderVo workOrderVo) {
        workorderService.updateWorkOrder(workOrderVo);
        return new ResponseEntity(new Result().ok("update success"), HttpStatus.OK);
    }

    /**
     * 完成任务
     *
     * @param workOrderVo 工单
     * @return 相应信息
     */
    @ApiOperation("接口描述：完成任务")
    @PutMapping("/workorder/accomplish")
    public ResponseEntity<Result> accomplish(@RequestBody @ApiParam(name = "workOrderVo", value = "工单视图对象") WorkOrderVo workOrderVo) {
        workorderService.accomplishWorkOrder(workOrderVo);
        return new ResponseEntity(new Result().ok("update success"), HttpStatus.OK);
    }
}
